package com.sunbeam.encryption.dac;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sunbeam.common.utils.DBUtils;
import com.sunbeam.encryption.paramObj.UserInfoEntity;

/**
 * 
 * @ClassName: UserInfoDao
 * @Description: TODO
 * @author AromaSunbeam lyzpoq_gmail_com
 * @date 2013-8-31 下午11:02:35
 *
 */
public class UserInfoDao {

	private Connection connection = null; // 定义连接的对象
	private PreparedStatement ps = null; // 定义预准备的对象

	public UserInfoDao() {
		DBUtils.getUrlParams();
		connection = DBUtils.getConnection(); // 利用构造方法取得数据库连接
	}

	/**
	 * 
	 * @Title: SelectTest
	 * @Description: TODO
	 * @param @return
	 * @return List
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	public List SelectTest() {
		ArrayList<UserInfoEntity> userInfos=new ArrayList<UserInfoEntity>();
		UserInfoEntity userInfo=new UserInfoEntity();
		try {
			ps = connection.prepareStatement("SELECT * FROM TB_USER_INFO");
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				userInfo=new UserInfoEntity();
				userInfo.setUID(rs.getString("UID"));
				userInfo.setUserName(rs.getString("USER_NAME"));
				userInfo.setPassword(rs.getBytes("PASSWORD"));
				userInfo.setEmail(rs.getString("EMAIL"));
				userInfo.setBirthday(rs.getDate("BIRTHDAY"));
				userInfos.add(userInfo);
				}
			ps.close();
		}
		 catch (SQLException ex) {
			ex.printStackTrace();
		}
		return userInfos;
	}
	
	/**
	 * 
	 * @Title: InsertTest
	 * @Description: TODO
	 * @param @param userInfo
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public boolean InsertTest(UserInfoEntity userInfo) {
		try {
			ps = connection.prepareStatement("INSERT INTO TB_USER_INFO (UID,USER_NAME,PASSWORD,EMAIL,BIRTHDAY) VALUES (?,?,?,?,?)");
			ps.setString(1, userInfo.getUID());
			ps.setString(2, userInfo.getUserName());
			ps.setBytes(3, userInfo.getPassword());
			ps.setString(4, userInfo.getEmail());
			ps.setDate(5, userInfo.getBirthday());
			ps.executeUpdate();
			ps.close();
			return true;
		} catch (SQLException ex) {
			return false;
		}
	}
	
	/**
	 * 
	 * @Title: UpdateTest
	 * @Description: TODO
	 * @param @param userInfo
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public boolean UpdateTest(UserInfoEntity userInfo) {
		try {
			ps = connection.prepareStatement("UPDATE TB_USER_INFO SET PASSWORD=? WHERE UID=?");
			ps.setBytes(1, userInfo.getPassword());
			ps.setString(2, userInfo.getUID());
			ps.executeUpdate();
			ps.close();
			return true;
		} catch (SQLException ex) {
			return false;
		}
	}
	
	/**
	 * 
	 * @Title: DeleteTest
	 * @Description: TODO
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public boolean DeleteTest() {
		try {
			ps = connection.prepareStatement("DELETE FROM TB_USER_INFO WHERE  UID<>?");
			ps.setString(1, "UID20130828001");
			ps.executeUpdate();
			ps.close();
			return true;
		} catch (SQLException ex) {
			return false;
		}
	}
}
